import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue'),
        name: 'Dashboard',
        meta: { title: 'Dashboard', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/submenu',
    component: Layout,
    redirect: '/submenu/about',
    meta: { title: 'submenu', icon: 'dashboard' },
    children: [
      {
        path: 'about',
        component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
        name: 'submenuAbout',
        meta: { title: 'About', icon: 'dashboard' }
      },
      {
        path: 'tableView',
        component: () => import(/* webpackChunkName: "tableView" */ '../views/tableView.vue'),
        name: 'tableView',
        meta: { title: 'tableView', icon: 'dashboard' }
      },
      {
        path: 'https://www.baidu.com',
        name: 'HAHAHA',
        meta: { title: '百度', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/other',
    component: Layout,
    redirect: '/other/about',
    alwaysShow: true,
    meta: { title: 'onlyonechild' },
    children: [
      {
        path: 'about',
        component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
        name: 'otherAbout',
        meta: { title: 'About', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/errorlog',
    children: [
      {
        path: 'errorlog',
        component: () => import(/* webpackChunkName: "error-log" */ '../views/error-log/index.vue'),
        name: 'error-log',
        meta: { title: 'error-log', icon: 'dashboard' }
      }
    ]
  }
]

export const asyncRoutes = []

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  scrollBehavior: () => ({ top: 0 }),
  routes: constantRoutes
})

export default router
